home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8041 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  61 lines

  1. Path: news.uoregon.edu!newsadmin@news.uoregon.edu
  2. From: Pat O'Connor <pwoc@darkwing.uoregon.edu>
  3. Newsgroups: comp.lang.c
  4. Subject: Something about typedef and functions I don't understand
  5. Date: 1 Mar 1996 07:30:12 GMT
  6. Organization: University of Oregon
  7. Message-ID: <4h6924$ihl@pith.uoregon.edu>
  8. NNTP-Posting-Host: darkwing.uoregon.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.4 sun4m)
  13. X-URL: news:comp.lang.c
  14.  
  15. I just ran into a problem using typedef for declaring aliases for function
  16. types.  Now, I thought that when I say for example,
  17.     typedef double (*DFunc)(double x);
  18. that I could use DFunc to declare (but not define) functions. But.... when
  19. I try to compile the following program, the linker has a fit. (granted,
  20. the program is pretty stupid - I just wanted to simplify things as much as 
  21. possible).
  22.  
  23. in file 1                in file 2
  24. --------------------------              --------------------------
  25. #include <stdio.h>            double f(double x) { return x*x;}
  26.  
  27. typedef double (*DFunc)(double x);
  28. double DoFunction(DFunc f, double x)
  29. {    return f(x);  }
  30.  
  31. extern DFunc f;
  32. main()
  33. {
  34.     printf("%f\n",DoFunction(f,3.0));
  35.     return 0;
  36. }
  37.  
  38. Now, if I replace the line   "extern DFunc f;" with the line
  39. "extern double f(double x);" the program compiles and runs fine.  So, 
  40. basically, my question is why is it an error to use "extern DFunc f;" 
  41. here?
  42.  
  43. Thanks.
  44.  
  45. Pat
  46.  
  47.  
  48.  
  49. -- 
  50. \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/ \/\/
  51.    ___     __     ___ 
  52.    \  \   /  \   /  /      Who says W is a boson? Why should W be relegated
  53.     \  \ /    \ /  /    to the end of the alphabet? Find out the truth
  54.      \     /\     /     long supressed by the elitist alphas and betas!
  55.       \   /  \   /      Send $10 and a SASE to: 
  56.        ---    ---
  57.         National Free W League
  58.         2820 High Street
  59.         Eugene, OR  97405
  60.  
  61.